
/**
 * 
 */
var nexthtml = {
	init:function(){
		
		
		$("body").click();
		var urlKey = chromeUtils.getYuming(chromeUtils.getUrl());
		if(urlKey){
			chrome.storage.sync.get(urlKey, function(res) {
				res = res[urlKey];
				if(res){
					if(res.switchOn){
						nexthtml.showNextBtn();
						console.info("初始化快捷播放程序");
		                nexthtml.setMessegeListener();
						nexthtml.setKeyDownListener();
					}
				}
			});
		}
	},
	
	setMessegeListener:function(){
		
	},
	
	setKeyDownListener : function(){
		 document.onkeydown=function(event){
            var e = event || window.event || arguments.callee.caller.arguments[0];
            if(e.ctrlKey ){
            	if(e.keyCode == 37){ // 左箭头
            		nexthtml.toNext(-1);
            	}else if(e.keyCode == 39){ // 右箭头
            		nexthtml.toNext(1);
            	}
            	
            }
        };
        setTimeout(function(){
        	nexthtml.findInIframe($("body"),$("body"));
        },5000);
        
	},
	
	showNextBtn:function(){
		var btnStyle = 'background:cadetblue; height: 24px;line-height:24px;padding: 0px 4px;';
		var btnClass = 'layui-btn layui-btn-primary';
		var toNext = "<button class='"+btnClass+"' style ='"+btnStyle+"' id='toNext'  >下一集</button>";
		var $eplanddiv = $("#eplanddiv");
		if($eplanddiv.length > 0){
			$eplanddiv.append(toNext);
		}else{
			$("html").append("<div id='eplanddiv' style='position:fixed;top:50px;right:30px;z-index: 999999;'>"+toNext+"</div>");
		}
		
		$("#toNext").off().click(function(e){
			nexthtml.toNext(1);
		});
	},
	removeNextBtn:function(){
		$("#toNext").remove();
	},
	toNext:function(type){
		var ntxt = null;
		if(type < 0){
			ntxt = "上"+type+"集";
		}else if(type > 0){
			ntxt = "下"+type+"集";
		}else{
			return ;
		}
		var url = chromeUtils.getUrl();
		var url2 = chromeUtils.getUrlF(url);
	    if(!url2.u1){
	    	alert("无法获取"+ntxt+"！！");
	    	return;
	    }
	    var u1 = url2.u1;
	    var u2 = url2.u2;
	    var spo = [{s:"-",t:1},{s:"_",t:1}]; //	t == 0取前，t==1 取后
	    var res = nexthtml.subStringAndChange(u2,{s:".",t:0},type);
	    
	    var furl = null,u2i= res.o;
	    if(res.o == u2){
	    	if(spo){
	    		var fix = false;
    			for(var ii =0; ii < spo.length;ii ++){
    				var res2 = nexthtml.subStringAndChange(res.t0,spo[ii],type);
    				if(res2.o != res.t0){
    					u2i = res2.o + res.t1;
    					fix = true;
    					break;
    				}
    			}
    			var pageInd = 0;
    			if(!fix){ // 可能通过参数来获取分页的
    				var urlParam = chromeUtils.getUrlParams(url);
    				if(urlParam){
    					for(var paramKey in urlParam){
    						var paramValue = urlParam[paramKey];
    						if(chromeUtils.isNumber(paramValue)){
    							u2i = "";
    							u1 = url.replace(paramKey+"="+paramValue,paramKey+"="+(paramValue-0+type));
    							break;
    						}
    					}
    				}
		        }
    		}
	    }
	    furl = u1 + u2i;
	    console.info(furl);
		if(furl)window.location.href = furl;
		
	},

	subStringAndChange:function(str,spo,ad){
		var res = {o:str,t0:str,t1:""};
		if(!str && !chromeUtils.isNumber(str)){
			return res;
		}

		var ind = str.indexOf(spo.s);
		if(ind > -1 && ind < str.length -1){
			var n1=null,n2 = null;
			if(spo.t==0){
				n1 = str.substring(0,ind);
	    	    n2 = str.substring(ind,str.length);
	    	    if(chromeUtils.isNumber(n1)){
	    	    	n1 = n1 -0 + ad;
	    	    }
			}else{
				ind ++;
				n1 = str.substring(0,ind);
	    	    n2 = str.substring(ind,str.length);
	    	    if(chromeUtils.isNumber(n2)){
	    	    	n2 = n2 -0 + ad;
	    	    }
			}
			res.o = n1 + n2;
			res.t0 = n1;
	    	res.t1 = n2;
		}else {
	    	if(chromeUtils.isNumber(str)){
	    		res.o = str - 0 + ad;
	    	}
	    }
		
		return res;
	},
	findInIframe : function($parent,top){
//		var iframes =  $parent.find("iframe");
//		nexthtml.middleClick($parent);
//		if(iframes.length > 0){
//			for(var i=0;i<iframes.length;i++){
//				var iframeTem = iframes.eq(i);
//				console.info(iframeTem)
////				var iframeTem = $(iframeDocument);
//				if(iframeTem.height() <= 0 ){
//					iframeTem.remove();
//				}else{
//					nexthtml.middleClick(iframeTem);
//					nexthtml.findInIframe(iframeTem,top);
//				}
//			}
//		}
		nexthtml.middleClick(document);
		for(var i=0;i< window.frames.length;i++){
			var f = window.frames[i];
//			nexthtml.middleClick(f.document);
		}
       
	}
	,middleClick:function(dom){
		dom.onmousedown = function(event) {
			console.info(event)
            // var event = event || window.event//兼容ie低版本的
            if (event.button == 0) {
                //鼠标左键 
                console.info(1)
            } else if (event.button == 2) {
                //鼠标右键 
               console.info(2)
            } else if (event.button == 1) {
                //鼠标中键 
                nexthtml.toNext(1);
                console.info("中键下一集");
                return false;
            } else if (event.button == 3) {
                //鼠标侧键3 
               console.info(3)
            } else if (event.button == 4) {
                //鼠标侧键4 
                console.info(4)
            }
        };
		
	}
}


nexthtml.init();

